package cn.fjy.android.phone;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import cn.fjy.android.phone.listener.GRPhoneStateListener;
import cn.fjy.android.phone.receiver.PhoneReceiver;
import cn.fjy.android.phone.worker.TalkTimeRemind;
import cn.fjy.android.res.Constant;
import cn.fjy.android.res.SysInfo;
import cn.fjy.android.utils.GRPropertiesUtil;

public class Main extends Activity {

	private boolean callOnVibratorFlag = true;;

	private int callOnVibratorTime = 100;

	private CheckBox callOnCheckBox;

	private CheckBox talkTimeRemind;

	private EditText callOnViTime_ET;

	private EditText ttRemindViratorTime;

	private RadioGroup ttRemindType;
	
	private Vibrator vibrator;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
		initWidget();
		initListener();
		initValue();
		connectVibratorConfig();
		talkTimeRemindConfig();
		
	}

	/**
	 * 初始化监听器
	 */
	private void initListener() {
		// 注册电话状态监听
		if (!SysInfo.phonestate_linsten) {
			TelephonyManager tm = (TelephonyManager) this
					.getSystemService(Context.TELEPHONY_SERVICE);
			tm.listen(new GRPhoneStateListener(vibrator),
					PhoneStateListener.LISTEN_CALL_STATE);
		}
	}

	/**
	 * 初始化视图控件
	 */
	private void initWidget() {
		callOnCheckBox = (CheckBox) this.findViewById(R.id.checkBox1);
		callOnCheckBox.setChecked(callOnVibratorFlag);

		talkTimeRemind = (CheckBox) this.findViewById(R.id.checkBox2);

		ttRemindType = (RadioGroup) this.findViewById(R.id.ttRemindType);

		ttRemindViratorTime = (EditText) this.findViewById(R.id.editText2);

		callOnViTime_ET = (EditText) this.findViewById(R.id.editText1);
		callOnViTime_ET.setText(callOnVibratorTime + "");

		((RadioButton) findViewById(R.id.radioButtonSY)).setChecked(true);
	}

	/**
	 * 初始化内部属性
	 */
	private void initValue() {
		GRPropertiesUtil prop = new GRPropertiesUtil(Constant.CONFIG_FILE_NAME);

		// 初始化接通震动开关
		String tempValue = prop.getValue(Constant.PROP_KEY_CALL_ON_VIBRATOR);
		if (tempValue != null && !"".equals(tempValue.trim())) {
			if (tempValue.equals(Constant.PROP_VALUE_CALL_ON_VIBRATOR)) {
				callOnVibratorFlag = true;
			} else {
				callOnVibratorFlag = false;
			}
		}

		// 初始化接通震动时间
		tempValue = prop.getValue(Constant.PROP_KEY_VIBRATOR_TIME);
		if (tempValue != null && !"".equals(tempValue.trim())) {
			try {
				callOnVibratorTime = Integer.valueOf(tempValue);
			} catch (Exception e) {
				callOnVibratorTime = 100;
			}
		}

		// 通话时间提示开关
		tempValue = prop.getValue(Constant.PROP_KEY_TALKTIME_REMIND);
		if (tempValue != null && !"".equals(tempValue.trim())) {
			if ("0".equals(tempValue)) {
				talkTimeRemind.setChecked(true);
			} else {
				talkTimeRemind.setChecked(false);
			}
		}

		// 通话时间提示方式
		tempValue = prop.getValue(Constant.PROP_KEY_TALKTIME_REMIND_TYPE);
		if (tempValue != null && !"".equals(tempValue.trim())) {
			if ((TalkTimeRemind.REMIND_TYPE_VIBRATOR + "").equals(tempValue)) {
				((RadioButton) findViewById(R.id.radioButtonZD))
						.setChecked(true);
				((RadioButton) findViewById(R.id.radioButtonSY))
						.setChecked(false);
				ttRemindViratorTime.setEnabled(true);
			} else {
				((RadioButton) findViewById(R.id.radioButtonSY))
						.setChecked(true);
				((RadioButton) findViewById(R.id.radioButtonZD))
						.setChecked(false);
				ttRemindViratorTime.setEnabled(false);
			}
		}

		// 初始化提示震动时间
		tempValue = prop.getValue(Constant.PROP_KEY_TT_VIBRATOR_TIME);
		if (tempValue != null && !"".equals(tempValue.trim())) {
			try {
				ttRemindViratorTime.setText(tempValue + "");
			} catch (Exception e) {
				ttRemindViratorTime.setText("100");
			}
		}

	}

	/**
	 * 保存用户配置
	 */
	private void saveConfig() {
		GRPropertiesUtil prop = new GRPropertiesUtil(Constant.CONFIG_FILE_NAME);
		prop.setValue(
				Constant.PROP_KEY_CALL_ON_VIBRATOR,
				callOnVibratorFlag == true ? Constant.PROP_VALUE_CALL_ON_VIBRATOR
						: "1");
		prop.setValue(Constant.PROP_KEY_VIBRATOR_TIME, callOnVibratorTime + "");

		prop.setValue(Constant.PROP_KEY_TALKTIME_REMIND,
				TalkTimeRemind.isEnable == true ? "0" : "1");

		if (TalkTimeRemind.isEnable) {
			prop.setValue(Constant.PROP_KEY_TALKTIME_REMIND_TYPE,
					TalkTimeRemind.remind_type + "");
			if (TalkTimeRemind.remind_type == TalkTimeRemind.REMIND_TYPE_VIBRATOR) {
				prop.setValue(Constant.PROP_KEY_TT_VIBRATOR_TIME,
						TalkTimeRemind.tt_vibrator_time + "");
			}
		}
	}

	/**
	 * 监听用户推出
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			getCallOnVibratorTime();
			getTTVibratorTime();
			saveConfig();
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 开启关闭接通震动
	 */
	private void connectVibratorConfig() {
		callOnCheckBox
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							callOnVibratorFlag = true;
						} else {
							callOnVibratorFlag = false;
						}
					}
				});
		PhoneReceiver.isEnable = callOnVibratorFlag;
	}

	/**
	 * 获取接通震动时间
	 */
	private void getCallOnVibratorTime() {
		String tempStr = callOnViTime_ET.getText().toString();
		try {
			callOnVibratorTime = Integer.valueOf(tempStr);
		} catch (Exception e) {
			callOnVibratorTime = 100;
		}
		if (callOnVibratorTime > 500 || callOnVibratorTime < 100) {
			callOnVibratorTime = 100;
		}
		PhoneReceiver.vibratorTime = callOnVibratorTime;
	}

	private void talkTimeRemindConfig() {
		// 是否开启通话时间提示
		talkTimeRemind
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							TalkTimeRemind.isEnable = true;
						} else {
							TalkTimeRemind.isEnable = false;
						}
					}
				});

		// 提示方式
		ttRemindType
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						if (R.id.radioButtonSY == checkedId) {
							TalkTimeRemind.remind_type = TalkTimeRemind.REMIND_TYPE_AUDIO;
							ttRemindViratorTime.setEnabled(false);
						} else if (R.id.radioButtonZD == checkedId) {
							TalkTimeRemind.remind_type = TalkTimeRemind.REMIND_TYPE_VIBRATOR;
							ttRemindViratorTime.setEnabled(true);
						}
					}
				});
	}

	/**
	 * 获取通话提示震动持续时间
	 */
	private void getTTVibratorTime() {
		String value = ttRemindViratorTime.getText().toString();
		int time = 100;
		try {
			time = Integer.valueOf(value);
			if (time > 500 || time < 100) {
				time = 100;
			}
		} catch (Exception e) {
			time = 100;
		}
		TalkTimeRemind.tt_vibrator_time = time;
	}

}